/**
 * 首页
 */
package com.xiaomuwu.android.ui.home

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.xiaomuwu.android.MainActivity
import com.xiaomuwu.android.R
import com.xiaomuwu.android.XiaoMuWuApplication
import com.xiaomuwu.android.ui.Displayer
import com.xiaomuwu.android.ui.ForSearch
import kotlinx.android.synthetic.main.content_fragment.*

class ContentFragment:Fragment(){

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.content_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        val adapter=ContentAdapter(this)
//        viewPager.adapter=adapter
        viewPager.adapter= ContentAdapter(this)
        viewPager.offscreenPageLimit=2
        viewPager.isUserInputEnabled=true
//        viewPager.currentItem=Displayer.page
//        adapter.notifyDataSetChanged()
//        viewPager.registerOnPageChangeCallback(object:ViewPager2.OnPageChangeCallback(){
//            override fun onPageSelected(position: Int) {
//                super.onPageSelected(position)
//                Displayer.page=position
//            }
//        })
        TabLayoutMediator(tabLayout,viewPager){tab,position->
            tab.text=when(position){
                0->"资料"
                1->"用户"
                else->""
            }
        }.attach()

        // 列表页面切换监听
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)

                // 收起键盘
                if (activity != null) {
                    val mainActivity = activity as MainActivity
                    mainActivity.hideKeyboard()
                }

                // 刷新资料/用户列表
                when (position) {
                    0 -> {
                        updateRes()
                    }
                    1 -> {
                        updateUser()
                    }
                }
            }
        })

        // 还原搜索框输入的内容
        searchContentEdit?.setText(if(ForSearch.contentName==null){
            ""
        }else{
            ForSearch.contentName
        })
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        // 搜索框内容变化监听
        searchContentEdit?.addTextChangedListener { editable ->
            // 更新搜索内容
            ForSearch.contentName=if(editable?.isNotBlank() == true) {
                editable.toString()
            }else {
                null
            }

            // 刷新资料/用户列表
            when (viewPager.currentItem) {
                0 -> {
                    updateRes()
                }
                1 -> {
                    updateUser()
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()
        ForSearch.contentName = null    // 重置搜索内容
    }

    /**
     * 刷新资料列表
     */
    private fun updateRes() {
        // 发送刷新资料列表广播
        val intent=Intent("com.xiaomuwu.android.UpdateRes")
        XiaoMuWuApplication.context.sendBroadcast(intent)
    }

    /**
     * 刷新用户列表
     */
    private fun updateUser() {
        // 发送刷新用户列表广播
        val intent=Intent("com.xiaomuwu.android.UpdateUser")
        XiaoMuWuApplication.context.sendBroadcast(intent)
    }

}

